package com.leetcode.test.array;

import org.junit.Assert;
import org.junit.Test;

/**
 * https://leetcode-cn.com/explore/learn/card/array-and-string/198/introduction-to-array/770/
 */
public class 中心索引 {

	@Test
	public void minSearch() {
		Assert.assertEquals(3, pivotIndex(new int[]{1, 7, 3, 6, 5, 6}));
		Assert.assertEquals(-1, pivotIndex(new int[]{1, 2, 3}));
	}

	public int pivotIndex(int[] nums) {
		for (int i = 0; i < nums.length; i++) {
			int leftCount  = 0;
			int rightCount = 0;

			for (int k = 0; k < i; k++) {
				leftCount += nums[k];
			}
			for (int k = i + 1; k < nums.length; k++) {
				rightCount += nums[k];
			}

			if (leftCount == rightCount) {
				return i;
			}
		}
		return -1;
	}
}
